<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Moral extends BaseController {

	public function __construct() {
        parent::__construct();
        // 后台初始化方法
        $this->admin_init();
        // 用户类型判断
        if($this->userType != USER_TYPE_SCHOOL && $this->userType != USER_TYPE_TEACHER) redirect(admin_url("login"));
        // 加载类库
        $this->load->model('schoolModel');
        $this->load->model('classesModel');
        $this->load->model('contentModel');
        $this->load->model('staffModel');
    }

	public function timeline_subject() {
        // 获取年级
        $gradeParams['schoolId'] = $this->rmxSchool['id'];
        $gradeParams['campusId'] = $this->campusInfo['id'];
        // 过滤软删除
        $gradeParams['isDelete'] = 0;
        $this->gradeList = $this->classesModel->get_grade_list($gradeParams);
        $this->display(ADMIN_DIR. DIRECTORY_SEPARATOR. "moral". DIRECTORY_SEPARATOR. "timeline_subject");
    }
    
	public function save_subject_action() {
        $subjectId = $this->post("id");
        $title = $this->post("title");
        $sort = $this->post("sort");
        
        // 参数检查
        if(!$title) throw new Exception("请填写栏目名称");
        // 提示信息
        $msg = $subjectId ? '修改栏目成功' : '新增栏目成功';
        // 入库
        $data['title'] = $title;
        $data['sort'] = $sort;
        $data['updatetime'] = date("Y-m-d H:i:s");
        if($subjectId) {
            $this->db->update(ContentModel::$moralTimelineSubjectTable, $data, "id='{$subjectId}'");
        } else {
            $data['school_id'] = $this->rmxSchool['id'];
            $data['campus_id'] = $this->campusInfo['id'];
            $data['is_delete'] = 0;
            $data['createtime'] = $data['updatetime'];
            $this->db->insert(ContentModel::$moralTimelineSubjectTable, $data);
        }
        
        // 返回值
        exit(json($msg, true));
    }

    // 获取动态栏目列表
    public function get_subject_list() {
        // 获取数据条数
        $totalParams['getRowNumber'] = true;
        // 过滤软删除
        $totalParams['isDelete'] = 0;
        $totalParams['schoolId'] = $this->rmxSchool['id'];
        $totalParams['campus_id'] = $this->campusInfo['id'];
        $total = $this->contentModel->get_moral_timeline_subject_list($totalParams);
        // 获取数据
        $data['pageStart'] = $this->get('start');
        $data['pageSize']= $this->get("length");
        $data['schoolId'] = $this->rmxSchool['id'];
        $data['campus_id'] = $this->campusInfo['id'];
        // 过滤软删除
        $data['isDelete'] = 0;
        $subjectList = $this->contentModel->get_moral_timeline_subject_list($data);

        // 返回值
        exit(admin_table_data($total, $subjectList));   
    }

    // 删除动态栏目
    public function delete_subject_action() {
        $id = $this->post('id');
        $data = array(
            'is_delete' => 1,
            'updatetime' => date('Y-m-d H:i:s')
        );
        $this->db->update(ContentModel::$moralTimelineSubjectTable, $data, array('id' => $id));
        exit(json('删除成功', true));
    }

    // 德育动态管理页面
    public function timeline() {
        // 获取栏目列表
        $data['schoolId'] = $this->rmxSchool['id'];
        $data['campus_id'] = $this->campusInfo['id'];
        // 过滤软删除
        $data['isDelete'] = 0;
        $this->subjectList = $this->contentModel->get_moral_timeline_subject_list($data);
        $this->display(ADMIN_DIR. DIRECTORY_SEPARATOR. "moral". DIRECTORY_SEPARATOR. "timeline");
    }
    
    // 动态编辑页面
    public function timeline_edit($id=0) {
        if($id) {
            $this->timeline = $this->contentModel->get_moral_timeline_info($id);
        }
        // 获取栏目列表
        $data['schoolId'] = $this->rmxSchool['id'];
        $data['campus_id'] = $this->campusInfo['id'];
        // 过滤软删除
        $data['isDelete'] = 0;
        $this->subjectList = $this->contentModel->get_moral_timeline_subject_list($data);
        // 生成又拍云签名
        require_once('vendor/autoload.php');
        $serviceConfig = new Upyun\Config(UP_BUCKET, UP_USERNAME, UP_PASSWORD);
        $serviceConfig->setFormApiKey(UP_FORM_API);
        $serviceConfig->getImageUploadParams(UP_FILEPATH, $this->policy, $this->signature);
        $this->display(ADMIN_DIR. DIRECTORY_SEPARATOR. "moral". DIRECTORY_SEPARATOR. "timeline_edit");
    }
    
    // 保存德育动态操作
	public function save_timeline_action() {
        $timelineId = $this->post("id");
        $title = $this->post("title");
        $subjectId = $this->post('subject_id');
        $sort = $this->post("sort") ? $this->post("sort") : 0;
        $content = $this->post("content");
        $cover = $this->post('cover');
        
        // 参数检查
        if(!$title) throw new Exception("请填写动态标题");
        if(!$subjectId) throw new Exception("请选择所属栏目");
        if(!$content) throw new Exception("请填写动态内容");
        if(!$cover) throw new Exception("请上传封面");
        // 提示信息
        $msg = $timelineId ? '修改动态成功' : '新增动态成功';
        // 入库
        $data['title'] = $title;
        $data['subject_id'] = $subjectId;
        $data['sort'] = $sort;
        $data['content'] = $content;
        $data['cover'] = $cover;
        $data['updatetime'] = date("Y-m-d H:i:s");
        if($timelineId) {
            $this->db->update(ContentModel::$moralTimelineTable, $data, "id='{$timelineId}'");
        } else {
            $data['school_id'] = $this->rmxSchool['id'];
            $data['campus_id'] = $this->campusInfo['id'];
            $data['is_delete'] = 0;
            $data['createtime'] = $data['updatetime'];
            $this->db->insert(ContentModel::$moralTimelineTable, $data);
        }
        
        // 返回值
        exit(json($msg, true));
    }

    // 获取动态动态列表
    public function get_timeline_list() {
        if(trim($this->get('title'))) {
            $totalParams['title'] = $data['title'] = $this->get('title');
        }
        if($this->get('subject_id')) {
            $totalParams['subjectId'] = $data['subjectId'] = $this->get('subject_id');
        }
        // 获取数据条数
        $totalParams['getRowNumber'] = true;
        // 过滤软删除
        $totalParams['isDelete'] = 0;
        $totalParams['schoolId'] = $this->rmxSchool['id'];
        $totalParams['campus_id'] = $this->campusInfo['id'];
        $total = $this->contentModel->get_moral_timeline_list($totalParams);
        // 获取数据
        $data['pageStart'] = $this->get('start');
        $data['pageSize']= $this->get("length");
        $data['schoolId'] = $this->rmxSchool['id'];
        $data['campus_id'] = $this->campusInfo['id'];
        // 过滤软删除
        $data['isDelete'] = 0;
        $timelineList = $this->contentModel->get_moral_timeline_list($data);
        foreach($timelineList as &$timeline) {
            // 获取栏目名称
            $subject = $this->contentModel->get_moral_timeline_subject_info($timeline['subject_id']);
            $timeline['subject_title'] = $subject['title'];
        }
        // 返回值
        exit(admin_table_data($total, $timelineList));   
    }

    // 删除动态动态
    public function delete_timeline_action() {
        $id = $this->post('id');
        $data = array(
            'is_delete' => 1,
            'updatetime' => date('Y-m-d H:i:s')
        );
        $this->db->update(ContentModel::$moralTimelineTable, $data, array('id' => $id));
        exit(json('删除成功', true));
    }

    // 德育活动管理页面
    public function activity() {
        // 获取年级
        $gradeParams['schoolId'] = $this->rmxSchool['id'];
        $gradeParams['campusId'] = $this->campusInfo['id'];
        // 过滤软删除
        $gradeParams['isDelete'] = 0;
        $this->gradeList = $this->classesModel->get_grade_list($gradeParams);
        // 获取年级对应的行政班级列表
        $gradeParams['classType'] = CLASSES_TYPE_NORMAL;
        $classData = $this->classesModel->get_class_list($gradeParams);
        $classArray = array();
        foreach($classData as $class) {
            $classArray[$class['grade_id']][] = array(
                "id" => $class['id'],
                "name" => $class['class_name']
            );
        }
        $this->gradeNormalClassList = json_encode($classArray);

        // 获取审核状态列表
        $this->stateList = array(
            array(
                'title' => transform_activity_state_to_chinese(MORAL_PENDING),
                'value' => MORAL_PENDING
            ),
            array(
                'title' => transform_activity_state_to_chinese(MORAL_ACCEPTED),
                'value' => MORAL_ACCEPTED
            ),
            array(
                'title' => transform_activity_state_to_chinese(MORAL_REJECTED),
                'value' => MORAL_REJECTED
            ),
        );
        $this->display(ADMIN_DIR. DIRECTORY_SEPARATOR. "moral". DIRECTORY_SEPARATOR. "activity");
    }
    // 德育活动详情页面
    public function activity_detail($id) {
        $activity = $this->contentModel->get_moral_activity_info($id);
        // 获取学生名称
        $student = $this->schoolModel->get_student_info($activity['student_id']);
        if($student) {
            $activity['student_realname'] = $student['realname'];
            // 获取班级名称
            $class = $this->classesModel->get_class_info($student['class_id']);
            $activity['class_name'] = $class['class_name'];
        }
        // 获取审核老师名称
        $teacher = $this->schoolModel->get_teacher_info($activity['remark_user_id']);
        $activity['teacher_realname'] = $teacher['realname'];
        // 获取审核状态
        $activity['state'] = transform_activity_state_to_chinese($activity['state']);
        // 获取材料图片
        if($activity['material']) {
            $activity['imgArr'] = explode(',', $activity['material']);
        }

        $this->activity = $activity;
        $this->display(ADMIN_DIR. DIRECTORY_SEPARATOR. "moral". DIRECTORY_SEPARATOR. "activity_detail");
    }
    // 德育活动编辑页面
    public function activity_edit($id=0) {
        if($id) {
            $activity = $this->contentModel->get_moral_activity_info($id);
            // 获取材料图片
            if($activity['material']) {
                $activity['imgArr'] = explode(',', $activity['material']);
            }
            $this->activity = $activity;
        }
        if($this->userType == USER_TYPE_TEACHER) {
            // 获取班主任对应的班级列表
            $coachParams['teacherId'] = $this->rmxUser['id'];
            $coachParams['coachType'] = COACH_TYPE_MASTER; 
            $coachList = $this->staffModel->get_coach_list($coachParams);
            if($coachList) {
                $studentList = array();
                foreach($coachList as $coach) {
                    // 获取班主任对应的班级学生
                    $stuParams['isDelete'] = 0;
                    // $stuParams['classId'] = $coach['class_id'];
                    $stuParams['classSet'] = $coach['class_ids'];
                    $arr = $this->schoolModel->get_student_list($stuParams);
                    if($arr) {
                        $studentList = array_merge($studentList, $arr);
                    }
                }
                if($studentList) {
                    foreach($studentList as $key => $student) {
                        // 获取首字母
                        $studentList[$key]['realnameFirstChar'] = getFirstChar($student['realname']);
                    }
                    // 按姓名首字母排序
                    $timeKey =  array_column($studentList, 'realnameFirstChar');
                    array_multisort($timeKey, SORT_ASC, $studentList); 
                }
                $this->studentList = $studentList;
            }
        }

        // 生成又拍云签名
        require_once('vendor/autoload.php');
        $serviceConfig = new Upyun\Config(UP_BUCKET, UP_USERNAME, UP_PASSWORD);
        $serviceConfig->setFormApiKey(UP_FORM_API);
        $serviceConfig->getImageUploadParams(UP_FILEPATH, $this->policy, $this->signature);
        $this->display(ADMIN_DIR. DIRECTORY_SEPARATOR. "moral". DIRECTORY_SEPARATOR. "activity_edit");
    }
    
    // 保存德育活动操作
	public function save_activity_action() {
        $activityId = $this->post("id");
        $studentId = $this->post('student_id');
        $actTime = $this->post("act_time");
        $duration = $this->post("duration");
        $mode = $this->post('mode');
        $dest = $this->post('dest');
        $content = $this->post('content');
        $harvest = $this->post('harvest');
        $remark = $this->post('remark');
        $materialArr = $this->post('material');
        // 参数检查
        if(!$studentId) throw new Exception("请选择学生");
        if(!$actTime) throw new Exception("请选择活动时间");
        if(!$duration) throw new Exception("请填写活动时长");
        if(!$mode) throw new Exception("请填写活动方式");
        if(!$dest) throw new Exception("请填写活动地点");
        if(!$content) throw new Exception("请填写活动内容");
        if(!$harvest) throw new Exception("请填写活动收获");
        if(!$remark) throw new Exception("请填写评语");
        if($materialArr) {
            if(9 < count($materialArr)) {
                throw new Exception("最多只能上传9张图片");
            }
            $material = implode(',', $materialArr);
        } else {
            $material = '';
        }
        // 提示信息
        $msg = $activityId ? '修改活动成功' : '新增活动成功';
        // 入库
        $data['student_id'] = $studentId;
        $data['act_time'] = $actTime;
        $data['duration'] = $duration;
        $data['mode'] = $mode;
        $data['dest'] = $dest;
        $data['content'] = $content;
        $data['harvest'] = $harvest;
        $data['remark'] = $remark;
        $data['material'] = $material;
        $data['updatetime'] = date("Y-m-d H:i:s");
        if($activityId) {
            $this->db->update(ContentModel::$moralActivityTable, $data, "id='{$activityId}'");
        } else {
            $data['school_id'] = $this->rmxSchool['id'];
            $data['campus_id'] = $this->campusInfo['id'];
            $data['is_delete'] = 0;
            $data['createtime'] = $data['updatetime'];
            // 状态默认已通过
            $data['state'] = MORAL_ACCEPTED;
            $data['is_self'] = 0;
            $data['remark_user_id'] = $this->rmxUser['id'];
            $this->db->insert(ContentModel::$moralActivityTable, $data);
            // 发送模板消息通知学生



            
        }
        // 返回值
        exit(json($msg, true));
    }

    // 获取活动活动列表
    public function get_activity_list() {
        // 如果是班主任登录，只查看本班级的学生
        if($this->userType == USER_TYPE_TEACHER) {
            // // 获取班主任对应的班级列表
            // $classList = $this->classesModel->get_class_list(array(
            //     'classMaster' => $this->rmxUser['id'],
            //     'isDelete' => 0
            // ));
            // if(empty($classList)) {
            //     // 该老师不是班主任
            //     exit(admin_table_data(0, array())); 
            // } else {
            //     if(count($classList) === 1) {
            //         // 只有一个班级
            //         $totalParams['classId'] = $data['classId'] = $classList[0]['id'];
            //     } else {
            //         $totalParams['classSet'] = $data['classSet'] = array_value_implode($classList, "id");
            //     }
            // }

            // 获取班主任对应的班级列表
            $coachParams['teacherId'] = $this->rmxUser['id'];
            $coachParams['coachType'] = COACH_TYPE_MASTER; 
            $coachList = $this->staffModel->get_coach_list($coachParams);
            if(empty($coachList)) {
                // 该老师不是班主任
                exit(admin_table_data(0, array())); 
            } else {
                if(count($coachList) === 1) {
                    // 只有一个班级
                    // $totalParams['classId'] = $totalParams['classId'] = $coachList[0]['class_id'];
                    $totalParams['classSet'] = $totalParams['classSet'] = $coachList[0]['class_ids'];
                } else {
                    $totalParams['classSet'] = $data['classSet'] = array_value_implode($coachList, "class_ids");
                }
            }
        }

        if($this->get('state')) {
            $totalParams['state'] = $data['state'] = $this->get('state');
        }
        if($this->get('realname')) {
            $totalParams['realname'] = $data['realname'] = $this->get('realname');
        }
        if($this->get('createdate')) {
            $totalParams['createdate'] = $data['createdate'] = $this->get('createdate');
        }
        // 学生班级筛选
        if($this->get('class_id')) {
            $totalParams['classId'] = $data['classId'] = $this->get('class_id');
        }
        // 学生年级筛选
        if(empty($this->get("class_id")) && $this->get("grade_id")) {
            $params['gradeId'] = $this->get("grade_id");
            $params['isDelete'] = 0;
            $classList = $this->classesModel->get_class_list($params);
            $totalParams['classSet'] = $data['classSet'] = array_value_implode($classList, "id");
        }
        // 获取数据条数
        $totalParams['getRowNumber'] = true;
        // 过滤软删除
        $totalParams['isDelete'] = 0;
        $totalParams['schoolId'] = $this->rmxSchool['id'];
        $totalParams['campus_id'] = $this->campusInfo['id'];
        $total = $this->contentModel->get_moral_activity_list($totalParams);
        // 获取数据
        $data['pageStart'] = $this->get('start');
        $data['pageSize']= $this->get("length");
        $data['schoolId'] = $this->rmxSchool['id'];
        $data['campus_id'] = $this->campusInfo['id'];
        // 过滤软删除
        $data['isDelete'] = 0;
        $activityList = $this->contentModel->get_moral_activity_list($data);
        if($activityList) {
            // 获取本校全部班级
            $classParams['schoolId'] = $this->rmxSchool['id'];
            $classParams['campusId'] = $this->campusInfo['id'];
            $classParams['isDelete'] = 0;
            $classList = $this->classesModel->get_class_list($classParams);
            $classList = array_value_to_key($classList, 'id');
            // 获取本校全部老师
            $teacherParams['schoolId'] = $this->rmxSchool['id'];
            $teacherParams['isDelete'] = 0;
            $teacherList = $this->schoolModel->get_teacher_list($teacherParams);
            $teacherList = array_value_to_key($teacherList, 'id');
            foreach($activityList as $key => &$activity) {
                if(!isset($classList[$activity['class_id']])) {
                    // 过滤已删除
                    unset($activityList[$key]);
                } else {
                    // 获取班级名称
                    $activity['class_name'] = $classList[$activity['class_id']]['class_name'];
                    // 获取审核老师名称
                    if($activity['remark_user_id']) {
                        $teacher = $teacherList[$activity['remark_user_id']];
                        $activity['teacher_realname'] = $teacher['realname'];
                    }
                    // 获取审核状态
                    $activity['stateFormat'] = transform_activity_state($activity['state']);
                }
            }
        }

        // 返回值
        exit(admin_table_data($total, $activityList));   
    }

    // 删除德育活动
    public function delete_activity_action() {
        $id = $this->post('id');
        $data = array(
            'is_delete' => 1,
            'updatetime' => date('Y-m-d H:i:s')
        );
        $this->db->update(ContentModel::$moralActivityTable, $data, array('id' => $id));
        exit(json('删除成功', true));
    }
    // 通过德育活动审核
    public function accept_activity_action() {
        $id = $this->post('id');
        $remark = trim($this->post('remark'));
        $data = array(
            'remark_user_id' => $this->rmxUser['id'],
            'state' => MORAL_ACCEPTED,
            'remark' => $remark,
            'updatetime' => date('Y-m-d H:i:s')
        );
        $this->db->update(ContentModel::$moralActivityTable, $data, array('id' => $id));
        exit(json('通过成功', true));
    }
    // 拒绝德育活动审核
    public function reject_activity_action() {
        $id = $this->post('id');
        $data = array(
            'remark_user_id' => $this->rmxUser['id'],
            'state' => MORAL_REJECTED,
            'updatetime' => date('Y-m-d H:i:s')
        );
        $this->db->update(ContentModel::$moralActivityTable, $data, array('id' => $id));
        exit(json('拒绝成功', true));
    }

    // 评价结果管理页面
    public function remark_result() {
        // 获取年级
        $gradeParams['schoolId'] = $this->rmxSchool['id'];
        $gradeParams['campusId'] = $this->campusInfo['id'];
        // 过滤软删除
        $gradeParams['isDelete'] = 0;
        $this->gradeList = $this->classesModel->get_grade_list($gradeParams);
        $this->display(ADMIN_DIR. DIRECTORY_SEPARATOR. "moral". DIRECTORY_SEPARATOR. "remark_result");
    }
    
    // 保存评价结果
	public function save_result_action() {
        $resultId = $this->post("id");
        $title = $this->post("title");
        $sort = $this->post("sort");
        
        // 参数检查
        if(!$title) throw new Exception("请填写结果名称");
        // 提示信息
        $msg = $resultId ? '修改评价结果成功' : '新增评价结果成功';
        // 入库
        $data['title'] = $title;
        $data['sort'] = $sort;
        $data['updatetime'] = date("Y-m-d H:i:s");
        if($resultId) {
            $this->db->update(ContentModel::$moralRemarkResultTable, $data, "id='{$resultId}'");
        } else {
            $data['school_id'] = $this->rmxSchool['id'];
            $data['campus_id'] = $this->campusInfo['id'];
            $data['is_delete'] = 0;
            $data['createtime'] = $data['updatetime'];
            $this->db->insert(ContentModel::$moralRemarkResultTable, $data);
        }
        
        // 返回值
        exit(json($msg, true));
    }

    // 获取评价结果列表
    public function get_result_list() {
        // 获取数据条数
        $totalParams['getRowNumber'] = true;
        // 过滤软删除
        $totalParams['isDelete'] = 0;
        $totalParams['schoolId'] = $this->rmxSchool['id'];
        $totalParams['campus_id'] = $this->campusInfo['id'];
        $total = $this->contentModel->get_moral_remark_result_list($totalParams);
        // 获取数据
        $data['pageStart'] = $this->get('start');
        $data['pageSize']= $this->get("length");
        $data['schoolId'] = $this->rmxSchool['id'];
        $data['campus_id'] = $this->campusInfo['id'];
        // 过滤软删除
        $data['isDelete'] = 0;
        $resultList = $this->contentModel->get_moral_remark_result_list($data);

        // 返回值
        exit(admin_table_data($total, $resultList));   
    }

    // 删除评价结果
    public function delete_result_action() {
        $id = $this->post('id');
        $data = array(
            'is_delete' => 1,
            'updatetime' => date('Y-m-d H:i:s')
        );
        $this->db->update(ContentModel::$moralRemarkResultTable, $data, array('id' => $id));
        exit(json('删除成功', true));
    }
    // 评价管理页面
    public function remark() {
        // 获取年级
        $gradeParams['schoolId'] = $this->rmxSchool['id'];
        $gradeParams['campusId'] = $this->campusInfo['id'];
        // 过滤软删除
        $gradeParams['isDelete'] = 0;
        $this->gradeList = $this->classesModel->get_grade_list($gradeParams);
        // 获取年级对应的行政班级列表
        $gradeParams['classType'] = CLASSES_TYPE_NORMAL;
        $classData = $this->classesModel->get_class_list($gradeParams);
        $classArray = array();
        foreach($classData as $class) {
            $classArray[$class['grade_id']][] = array(
                "id" => $class['id'],
                "name" => $class['class_name']
            );
        }
        $this->gradeNormalClassList = json_encode($classArray);
        // 获取评价结果列表
        $resultParams['schoolId'] = $this->rmxSchool['id'];
        $resultParams['campusId'] = $this->campusInfo['id'];
        $resultParams['isDelete'] = 0;
        $this->resultList = $this->contentModel->get_moral_remark_result_list($resultParams);

        $this->display(ADMIN_DIR. DIRECTORY_SEPARATOR. "moral". DIRECTORY_SEPARATOR. "remark");
    }
    
    // 德育评价编辑页面
    public function remark_edit($id=0) {
        if($id) {
            $remark = $this->contentModel->get_moral_remark_info($id);
            // 获取材料图片
            if($remark['material']) {
                $remark['imgArr'] = explode(',', $remark['material']);
            }
            $this->remark = $remark;
        }
        if($this->userType == USER_TYPE_TEACHER) {
            // 获取班主任对应的班级列表
            $coachParams['teacherId'] = $this->rmxUser['id'];
            $coachParams['coachType'] = COACH_TYPE_MASTER; 
            $coachList = $this->staffModel->get_coach_list($coachParams);
            if($coachList) {
                $studentList = array();
                foreach($coachList as $coach) {
                    // 获取班主任对应的班级学生
                    $stuParams['isDelete'] = 0;
                    // $stuParams['classId'] = $coach['class_id'];
                    $stuParams['classSet'] = $coach['class_ids'];
                    $arr = $this->schoolModel->get_student_list($stuParams);
                    if($arr) {
                        $studentList = array_merge($studentList, $arr);
                    }
                }
                if($studentList) {
                    foreach($studentList as $key => $student) {
                        // 获取首字母
                        $studentList[$key]['realnameFirstChar'] = getFirstChar($student['realname']);
                    }
                    // 按姓名首字母排序
                    $timeKey =  array_column($studentList, 'realnameFirstChar');
                    array_multisort($timeKey, SORT_ASC, $studentList); 
                }
                $this->studentList = $studentList;
            }
        }
        // 获取评价结果列表
        $resultParams['schoolId'] = $this->rmxSchool['id'];
        $resultParams['campusId'] = $this->campusInfo['id'];
        $resultParams['isDelete'] = 0;
        $this->resultList = $this->contentModel->get_moral_remark_result_list($resultParams);
        // 生成又拍云签名
        require_once('vendor/autoload.php');
        $serviceConfig = new Upyun\Config(UP_BUCKET, UP_USERNAME, UP_PASSWORD);
        $serviceConfig->setFormApiKey(UP_FORM_API);
        $serviceConfig->getImageUploadParams(UP_FILEPATH, $this->policy, $this->signature);
        $this->display(ADMIN_DIR. DIRECTORY_SEPARATOR. "moral". DIRECTORY_SEPARATOR. "remark_edit");
    }

    // 保存评价
	public function save_remark_action() {
        $remarkId = $this->post("id");
        $studentId = $this->post('student_id');
        $resultId = $this->post('result_id');
        $content = $this->post("content");
        $materialArr = $this->post("material");
        if(empty($studentId)) {
            throw new Exception("请选择学生");
        }
        if(empty($resultId)) {
            throw new Exception("请选择评价结果");
        }
        if(empty($content)) {
            throw new Exception("请填写评价内容");
        }
        if($materialArr) {
            if(9 < count($materialArr)) {
                throw new Exception("最多只能上传9张图片");
            }
            $material = implode(',', $materialArr);
        } else {
            $material = '';
        }
        
        // 提示信息
        $msg = $remarkId ? '修改评价成功' : '新增评价成功';
        // 入库
        $data['student_id'] = $studentId;
        $data['result_id'] = $resultId;
        $data['content'] = $content;
        $data['material'] = $material;
        $data['updatetime'] = date("Y-m-d H:i:s");
        if($remarkId) {
            $this->db->update(ContentModel::$moralRemarkTable, $data, "id='{$remarkId}'");
        } else {
            // 判断是否已存在改学生评价
            $remark = $this->contentModel->get_moral_remark_list(array(
                'isDelete' => 0,
                'studentId' => $studentId
            ));
            if($remark) {
                throw new Exception('该学生德育评价已存在');
            }
            $data['school_id'] = $this->rmxSchool['id'];
            $data['campus_id'] = $this->campusInfo['id'];
            $data['is_delete'] = 0;
            $data['createtime'] = $data['updatetime'];
            $this->db->insert(ContentModel::$moralRemarkTable, $data);
        }
        
        // 返回值
        exit(json($msg, true));
    }

    // 获取评价列表
    public function get_remark_list() {
        // 如果是班主任登录，只查看本班级的学生
        if($this->userType == USER_TYPE_TEACHER) {
            // // 获取班主任对应的班级列表
            // $classList = $this->classesModel->get_class_list(array(
            //     'classMaster' => $this->rmxUser['id'],
            //     'isDelete' => 0
            // ));
            // if(empty($classList)) {
            //     // 该老师不是班主任
            //     exit(admin_table_data(0, array())); 
            // } else {
            //     if(count($classList) === 1) {
            //         // 只有一个班级
            //         $totalParams['classId'] = $data['classId'] = $classList[0]['id'];
            //     } else {
            //         $totalParams['classSet'] = $data['classSet'] = array_value_implode($classList, "id");
            //     }
            // }

            // 获取班主任对应的班级列表
            $coachParams['teacherId'] = $this->rmxUser['id'];
            $coachParams['coachType'] = COACH_TYPE_MASTER; 
            $coachList = $this->staffModel->get_coach_list($coachParams);
            if(empty($coachList)) {
                // 该老师不是班主任
                exit(admin_table_data(0, array())); 
            } else {
                if(count($coachList) === 1) {
                    // 只有一个班级
                    $totalParams['classSet'] = $data['classSet'] = $coachList[0]['class_ids'];
                } else {
                    $totalParams['classSet'] = $data['classSet'] = array_value_implode($coachList, "class_ids");
                }
            }
        }
        if($this->get('result')) {
            $totalParams['resultId'] = $data['resultId'] = $this->get('result');
        }
        if($this->get('realname')) {
            $totalParams['realname'] = $data['realname'] = $this->get('realname');
        }
        if($this->get('createdate')) {
            $totalParams['createdate'] = $data['createdate'] = $this->get('createdate');
        }
        // 学生班级筛选
        if($this->get('class_id')) {
            $totalParams['classId'] = $data['classId'] = $this->get('class_id');
        }
        // 学生年级筛选
        if(empty($this->get("class_id")) && $this->get("grade_id")) {
            $params['gradeId'] = $this->get("grade_id");
            $params['isDelete'] = 0;
            $classList = $this->classesModel->get_class_list($params);
            $totalParams['classSet'] = $data['classSet'] = array_value_implode($classList, "id");
        }
        
        // 获取数据条数
        $totalParams['getRowNumber'] = true;
        // 过滤软删除
        $totalParams['isDelete'] = 0;
        $totalParams['schoolId'] = $this->rmxSchool['id'];
        $totalParams['campus_id'] = $this->campusInfo['id'];
        $total = $this->contentModel->get_moral_remark_list($totalParams);
        // 获取数据
        $data['pageStart'] = $this->get('start');
        $data['pageSize']= $this->get("length");
        $data['schoolId'] = $this->rmxSchool['id'];
        $data['campus_id'] = $this->campusInfo['id'];
        // 过滤软删除
        $data['isDelete'] = 0;
        $remarkList = $this->contentModel->get_moral_remark_list($data);
        if($remarkList) {
            // 获取本校全部班级
            $classParams['schoolId'] = $this->rmxSchool['id'];
            $classParams['campusId'] = $this->campusInfo['id'];
            $classParams['isDelete'] = 0;
            $classList = $this->classesModel->get_class_list($classParams);
            $classList = array_value_to_key($classList, 'id');
            // 获取评价结果列表
            $resultParams['schoolId'] = $this->rmxSchool['id'];
            $resultParams['campusId'] = $this->campusInfo['id'];
            $resultParams['isDelete'] = 0;
            $resultList = $this->contentModel->get_moral_remark_result_list($resultParams);
            $resultList = array_value_to_key($resultList, 'id');
            foreach($remarkList as &$remark) {
                // 获取班级名称
                $remark['class_name'] = $classList[$remark['class_id']]['class_name'];
                // 获取评价结果名称
                $remark['resultFormat'] = $resultList[$remark['result_id']]['title'];
                // 获取材料图片
                if($remark['material']) {
                    $remark['imgArr'] = explode(',', $remark['material']);
                } else {
                    $remark['imgArr'] = array();
                }
            }
        }
        // 返回值
        exit(admin_table_data($total, $remarkList));   
    }

    // 删除评价
    public function delete_remark_action() {
        $id = $this->post('id');
        $data = array(
            'is_delete' => 1,
            'updatetime' => date('Y-m-d H:i:s')
        );
        $this->db->update(ContentModel::$moralRemarkTable, $data, array('id' => $id));
        exit(json('删除成功', true));
    }
}
